package com.gang.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


import com.gang.model.Constant;
import com.gang.model.TParam;
import com.gang.model.WAttachment;
import com.gang.model.WCategorylink;
import com.gang.model.WDoc;

public class WDocUtil {
	private static DbUtilsTemplate dt=new DbUtilsTemplate();
	public static boolean addWDoc(int id){
		try {
			Constant.max_id++;
			List<WCategorylink> catLinks=new ArrayList<WCategorylink>();
			System.out.println("抓取地址"+Constant.wikiSite+"doc-view-"+id+".html");
			Document doc = Jsoup.connect(Constant.wikiSite+"doc-view-"+id+".html").get();
			
			//处理分类begin
			Elements catS=doc.getElementById("catenavi").getElementsByTag("a");
			for(Element cat:catS){
				WCategorylink catLink = new WCategorylink();
				catLink.setCid(CatUtil.getCatId(cat.text()));
				catLinks.add(catLink);
			}
			//处理分类end
			//获取doc begin
			WDoc wDoc = new WDoc();
			Element doctitle = doc.getElementById("doctitle");
//			System.out.println(doctitle.text());
			WDoc d=dt.findFirst
					(WDoc.class, "select * from `wiki_doc` where wiki_doc.title='"+doctitle.text()+"'");
			if(d!=null){
				System.out.println("已经存在");
				return true;
			}
			
			wDoc.setTitle(doctitle.text());
			wDoc.setLetter(PinyinConv.cn2py(doctitle.text().substring(0, 1)));
			wDoc.setAuthor("skyboy");
			wDoc.setAuthorid(2);
			wDoc.setTime(new Date().getTime()/1000);
			wDoc.setLastedit(new Date().getTime()/1000);
			wDoc.setLasteditorid(2);
			wDoc.setLasteditor("skyboy");
			wDoc.setVisible(1);
			wDoc.setLocked(0);
			
			Elements  texts = doc.getElementsByClass("texts");
			Elements  content_topps = doc.getElementsByClass("content_topp");
			int cha=content_topps.size()-texts.size();
			
			String allcontent="";
			for (int i = 0; i < texts.size(); i++) {
				String content="";
				Element content1 =texts.get(i);
				Element content2 = content_topps.get(i+cha);
				content= "<div class=\"hdwiki_tmml\">"+content1.text()+"</div>"+content2.html();
				allcontent+=content;
			}
			if(allcontent.length()<100){
				System.out.println("内容过少");
				return false;
			}
			allcontent=allcontent.replaceAll("你所看到的资源来自www.avtiantang.cc av天堂", "");
			allcontent=allcontent.replaceAll("<a href=\"wiki/", "<a href=\"index.php?search-fulltext-title-");
			allcontent=allcontent.replaceAll("<a href=\"http://www.avtiantang.cc/wiki/", "<a href=\"index.php?search-fulltext-title-");
			allcontent=allcontent.replaceAll("class=\"baikelink\"", "--all-0-within-time-desc-1  class=\"baikelink\"");
			allcontent=allcontent.replaceAll("\" --all-0-within-time-desc-1", "--all-0-within-time-desc-1\"");
			allcontent=allcontent.replaceAll("title-H.M.P--all", "title-H%26%2346%3BM%26%2346%3BP--all");
			wDoc.setContent(allcontent);
			String sql="INSERT INTO `wiki_doc` (wiki_doc.cid,wiki_doc.letter,wiki_doc.title,wiki_doc.tag,wiki_doc.summary," +
					"wiki_doc.content,wiki_doc.author,wiki_doc.authorid,wiki_doc.time,wiki_doc.lastedit,wiki_doc.lasteditor," +
					"wiki_doc.lasteditorid,wiki_doc.views,wiki_doc.edits,wiki_doc.editions,wiki_doc.comments,wiki_doc.votes," +
					"wiki_doc.visible,wiki_doc.locked) VALUES (" +
					"?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
			Object[] params={wDoc.getCid(),wDoc.getLetter(),wDoc.getTitle(),wDoc.getTag(),wDoc.getSummary(),wDoc.getContent()
					,wDoc.getAuthor(),wDoc.getAuthorid(),wDoc.getTime(),wDoc.getLastedit(),wDoc.getLasteditor(),wDoc.getLasteditorid()
					,wDoc.getViews(),wDoc.getEdits(),wDoc.getEditions(),wDoc.getComments(),wDoc.getVotes(),wDoc.getVisible(),wDoc.getLocked()};
			dt.update(sql, params);
			
			int did=dt.findFirst(WDoc.class, "select * from `wiki_doc` where wiki_doc.title='"+wDoc.getTitle()+"' and wiki_doc.lastedit="+wDoc.getLastedit()).getDid();
			wDoc.setDid(did);
			//获取doc end
			//保存分类begin
			for (Iterator iterator = catLinks.iterator(); iterator.hasNext();) {
				WCategorylink wCategorylink = (WCategorylink) iterator.next();
				wCategorylink.setDid(wDoc.getDid());
				sql="insert into wiki_categorylink (did,cid) values (?,?)";
				Object[] params2={wCategorylink.getDid(),wCategorylink.getCid()};
				dt.update(sql, params2);
			}
			//保存分类end
			//保存图片
			List<WAttachment> ts=new ArrayList<WAttachment>();
			Elements imgs=doc.getElementsByTag("img");
			for (Element img : imgs) {
				String src=img.attr("src");
				if(src.contains("upload")){
					WAttachment t=null;
					if(src.contains(Constant.wikiSite)){
						t=ImageUtils.downPic(src);
					}else{
						t=ImageUtils.downPic(Constant.wikiSite+"/"+src);
					}
					t.setDid(wDoc.getDid());
					t.setDescription("");
					t.setUid(2);
					if(t.getFilename().equals("1.png")){
						continue;
					}
					sql="insert into `wiki_attachment` (did,time,filename,description,filetype,filesize,attachment,downloads," +
							"coindown,isimage,uid,state,focus) values(?,?,?,?,?,?,?,?,?,?,?,?,?) ";
					Object[] params2={t.getDid(),t.getTime(),t.getFilename(),t.getDescription(),t.getFiletype(),t.getFilesize(),
							t.getAttachment(),t.getDownloads(),t.getCoindown(),t.getIsimage(),t.getUid(),t.getState(),t.getFocus()};
					dt.update(sql, params2);
				}
				
			}
			dt.update("update `t_param` set `value`='"+(Constant.max_id-1)+"'");
			//保存图片
			return true;
		} catch (IOException e) {
//			e.printStackTrace();
			System.out.println("1:"+e.getLocalizedMessage());
			//保存好当前的最大值
			
			return false;
		}catch (Exception e) {
//			e.printStackTrace();
			System.out.println("2:"+e.getLocalizedMessage());
			return false;
		}
	}
	public static void main(String[] args) {
//		int time=Integer.parseInt(System.getProperty("time"));;//多久抓取一次
		int time=Integer.parseInt(args[0]);;//多久抓取一次
		Timer timer=new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				try {
					TParam param=dt.findFirst(TParam.class, "SELECT * FROM `t_param` where `key`='baiyou'");
					int from=Integer.parseInt(param.getValue());
					Constant.max_id=from;
					while (addWDoc(Constant.max_id)) {
						System.out.println(Constant.max_id+",");
					}
//					System.out.println(".");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		},  2 * 1000, time * 1000*60*60);//一个小时抓一次
		
	}
}
